﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QZone.crlclr.album;
using System.IO;
using System.Threading;
using QZone.crlclr.errors;

namespace QZone
{
    /// <summary>
    ///  单参数代理类;
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="value"></param>
    internal delegate void TempDelegate<T>(T value);


    public partial class Form1 : Form
    {
        private string qqcode;
        public Form1()
        {
            
            InitializeComponent();
        }

        /// <summary>
        ///  注册下载文件事件;
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            QQZoneManager.getInstance().onDownLoadFileProgress += new DownLoadFileProgress(onDownLoadFileProgress);
        }

        void onDownLoadFileProgress(string fileName)
        {
            TempDelegate<string> dele = (name) => toolStripStatusLabel1.Text = string.Format("下载文件:{0}", name);
            qqStatusStrip.Invoke(dele, fileName);
        }


        /// <summary>
        ///  开始进行下载;
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getPhotoBtn_Click(object sender, EventArgs e)
        {
            qqcode = qqcodeTb.Text.Trim();
           
            if (qqcode.Length < 5)
            {
                MessageBox.Show("请输入QQ号码");
            }

            toolStripStatusLabel1.Text = "开始下载配置文件";
            QQZoneManager.isNews = isNewestCb.Checked;

            ThreadPool.QueueUserWorkItem(new WaitCallback(startGetQQPhotos));
        }


        private string _savePath;
        public string savePath
        {
            set{
                _savePath=Path.Combine(Application.StartupPath,value);
            }
            get{
                return _savePath;
            }

        }

        /// <summary>
        /// 开个新线程做这方面的操作;
        /// </summary>
        /// <param name="info"></param>
        private void startGetQQPhotos(object info)
        {

            TempDelegate<bool> dele = (enabled) => this.getPhotoBtn.Enabled = enabled;
            
            this.getPhotoBtn.Invoke(dele,false);

            savePath = qqcode;

            QQZoneManager qq = QQZoneManager.getInstance();

            try
            {
                qq.download(qqcode, savePath);
            }
            catch (NetLoadError error)
            {
                MessageBox.Show(error.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("未知错误,{0}", ex.Message));
            }
            finally
            {
                this.getPhotoBtn.Invoke(dele, true);
            }

            MessageBox.Show("完成提取");
        }

        
    }
}
